package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.AjaxResult;
import com.example.demo.common.TableDataInfo;
import com.example.demo.controller.base.BaseController;
import com.example.demo.domain.model.Book;
import com.example.demo.domain.model.User;
import com.example.demo.domain.query.*;
import com.example.demo.mapping.BookMapping;
import com.example.demo.mapping.UserMapping;
import com.example.demo.service.IBookService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController extends BaseController {
    @Autowired
    private IBookService bookService;
    @GetMapping("/list")
    public TableDataInfo list(PageQuery pageQuery){
        Page<Book> page = new Page<>(pageQuery.getPageNum(),pageQuery.getPageSize());
        Page<Book> page1 = bookService.page(page);
        return dataInfo(page1.getRecords(),page1.getTotal());
    }

    @GetMapping("/dtl/{id}")
    public AjaxResult dtl(@PathVariable Long id){
        Book book = bookService.getById(id);
        return AjaxResult.success(book);
    }


    @PostMapping
    public AjaxResult createBook(@RequestBody BookCreateQuery bookCreateQuery){

        Book create = BookMapping.INSTANCE.toCreate(bookCreateQuery);
        return toAjax(bookService.save(create)) ;
    }

    @PutMapping
    public AjaxResult updateBook(@RequestBody BookUpdateQuery bookUpdateQuery){

        Book update = BookMapping.INSTANCE.toUpdate(bookUpdateQuery);
        return toAjax(bookService.updateById(update)) ;
    }

    @DeleteMapping("/{bookId}")
    public AjaxResult deleteBook(@PathVariable Long bookId){

        return toAjax(bookService.removeById(bookId)) ;
    }
    @GetMapping("/notreturn")
    public TableDataInfo getBookList(PageQuery pageQuery){
        com.github.pagehelper.Page<Object> page = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        bookService.getBookList();
        return dataInfo(page.getResult(),page.getTotal());

    }
    @GetMapping("/return/{bookIds}")
    public AjaxResult updateStatuses(@PathVariable List<Long> bookIds){
        return toAjax(bookService.updateStatuses(bookIds));
    }
}
